package com.cbarmstrong.FinanceWeb;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cbarmstrong.Finance.Data;

/**
 * Servlet implementation class Report
 */
@WebServlet("/Report")
public class Report extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Report() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connection c;
		Statement s;
		ResultSet r;
		int stock;
		boolean action;
		String tabHighlight, tabStyle, actionTable="", holdTable="", outTable="", header;
		double pos, lastPos;
		double[] todaysDist;
		PopulationDistribution pd = new PopulationDistribution();
		try {
			c = Data.initConnection();
			s = c.createStatement();
			r= s.executeQuery("SELECT id, name FROM stock");
			header="<TABLE WIDTH=900>";
			header+="<TR><TD WIDTH=400>Stock<TD>Buy<TD>Hold<TD>Sell</TR>";
			while(r.next()){
				stock=r.getInt("id");
				todaysDist=pd.getDistribution(stock);
				pos=todaysDist[3];
				lastPos=pd.getHistoricDistribution(stock, 1)[3];
				tabHighlight="background-color:white;";
				tabStyle="font-weight: normal;";
				action=false;
				if(pos==1) tabHighlight="background-color:green;";
				if(pos==-1) tabHighlight="background-color:red;";
				if(pos!=lastPos){ tabStyle="font-weight: bold;"; action=true; }
				String line="<TR style='"+tabStyle+tabHighlight+"'><TD>"+r.getString("name")+"<TD>"+todaysDist[0]+"<TD>"+todaysDist[1]+"<TD>"+todaysDist[2]+"<TD>+"+todaysDist[4]+"</TR>";
				if(action){ actionTable+=line; }
				else{
					if(pos!=0){ holdTable+=line; }
					else{ outTable+=line; }
				}
			
			}
			actionTable=header+actionTable+"</TABLE>";
			holdTable=header+holdTable+"</TABLE>";
			outTable=header+outTable+"</TABLE>";
		}
		catch(Exception e){
			e.printStackTrace();
		}
		response.setHeader("content-type", "text/html");
		response.getWriter().write("<HTML><BODY>");
		response.getWriter().write("<BR><BR><B>Actions:</B><BR><BR>");
		response.getWriter().write(actionTable);
		response.getWriter().write("<BR><BR><B>Holding Positions:</B><BR><BR>");
		response.getWriter().write(holdTable);
		response.getWriter().write("<BR><BR><B>No Position:</B><BR><BR>");
		response.getWriter().write(outTable);
		response.getWriter().write("</BODY></HTML>");
	}

}
